import sys,os
sys.path.append(os.path.realpath('../myfunctions'))
import fast_network_analysis as fna
import netclass_cluster as nc
import net_analysis as na

import numpy
from scipy import *
from brian import *
import time

# ------------------- DICTIONARIES

Net_par={}
Syn_exc_par={}
Syn_inh_par={}
Syn_x_par={}
Pas_par={}
Fr_par={}
Init_par={}
Sim_par={}

# ------------------- NETWORK PARAMETERS
Net_par['Ne']=3800;
Net_par['Nc']=200;
Net_par['Ni']=1000;
Net_par['Nx']=4000;
Net_par['Ny']=4000;

Net_par['Pee']=0.1;
Net_par['Pie']=0.1;
Net_par['Pei']=0.1;
Net_par['Pii']=0.1;
Net_par['Pex']=0.1;
Net_par['Pix']=0.1;

Net_par['Pcc']=0.1;



# ------------------- SYNAPTIC PARAMETERS



# --------- Excitatory
Syn_exc_par['tau']=0.002; # seconds
Syn_exc_par['Bee']=0.008*exp(1); #units of Gl
Syn_exc_par['Beestd']=Syn_exc_par['Bee']*0.8; 
Syn_exc_par['Bie']=0.016*exp(1); #units of Gl
Syn_exc_par['Biestd']=Syn_exc_par['Bie']*0.8; 

Syn_exc_par['WeightDist']='Normal'; #Normal/LogNormal


# --------- Inhibitory
Syn_inh_par['tau']=0.002; # seconds
Syn_inh_par['Bei']=0.075*exp(1); # units of Gl
Syn_inh_par['Beistd']=Syn_inh_par['Bei']*0.8; 
Syn_inh_par['Bii']=0.075*exp(1); # units of Gl
Syn_inh_par['Biistd']=Syn_inh_par['Bii']*0.8; 

Syn_inh_par['WeightDist']='Normal'; #Normal/LogNormal

# --------- External Drive
Syn_x_par['tau']=0.002 # seconds
Syn_x_par['Bex']=0.04*exp(1)  # units of gl
Syn_x_par['Bexstd']=Syn_x_par['Bex']*0.8 
Syn_x_par['Bix']=0.04*exp(1)  # units of Gl
Syn_x_par['Bixstd']=Syn_x_par['Bix']*0.8 

Syn_x_par['WeightDist']='Normal' #Normal/LogNormal

# ---------------------------- PASSIVE PARAMETERS
Pas_par['C']=100. *10**(-12)
Pas_par['gl']=5.55 *10**(-9)

Pas_par['Ee']=0.
Pas_par['Ei']=-75.
Pas_par['El']=-62.

Pas_par['thr']=-48.
Pas_par['thr_res']=-52.
Pas_par['tau_ref']=5.

# ---------------------------- FREQUENCY PAR

Fr_par['x']=5.
Fr_par['y']=0.025



# ---------------------------- INITIALIZATION PAR
Init_par['V']=(-60.,0.01) # mean, std of gaussian distribution
Init_par['ge']=(0.,0.001)
Init_par['gi']=(0.,0.001)

# ---------------------------- SIMULATION PAR

Sim_par['duration']=1000.
Sim_par['Nruns']=1
Sim_par['lapse']=0. # how many seconds are recorded in the volt monitor

Sim_par['Monitors_exc_size']=0
Sim_par['Monitors_inh_size']=0
Sim_par['Monitors_clu_size']=0

Sim_par['Simulation_Clock']= 0.1 * ms
Sim_par['Monitor_Clock']= 0.1 * ms


# ---------------------------- STARTING

nm1=nc.IFnetwork(Net_par,Syn_exc_par,Syn_inh_par,Syn_x_par,Pas_par,Fr_par,Init_par,Sim_par)


nm1.buildPopulation()

nm1.buildConnection()

nm1.buildMonitors(Sim_par['Monitors_exc_size'],Sim_par['Monitors_inh_size'],Sim_par['lapse'])

nm1.initialParam()

nm1.buildNetwork()

run_n=np.arange(1,Sim_par['Nruns']+1)

nm1.saveParam()

import pdb; pdb.set_trace()

for nr in run_n:

    print '\nRunning simulation:'+str(nr)+'/'+str(Sim_par['Nruns'])+'\n'
    nm1.runSim(Sim_par['duration'])
    nm1.saveSpikes(nr)
    nm1.saveRates(nr)
    nm1.saveMonitors(nr)
    nm1.resetSpMon()

nm1.saveLog()   

print 'Filename: ' + str(nm1.filename) + '\n'

#nm1.grabResults()



# nm1.loop1D(1,0,10,11)

# nm1.save_loop('fr_loop','../datasave/') # ('filename','folder/')






